<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\validate\UserValidate;
use app\http\GetUid;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\facade\Cache;
use think\facade\Request;
use think\facade\Validate;

class User extends Common
{
    use GetUid;

    /**
     * @param Request $request
     * @param Admin $admin
     * @param UserValidate $validate
     * @return \think\response\Json
     */
    public function userUpdate(Request $request, Admin $admin, UserValidate $validate)
    {
        if (!$validate->check($request::param())) {
            return $this->returnJson(422, $validate->getError());
        }
        try {
            $user = $admin->findOrFail(self::getUId());
            $res = $user->save($request::only(['username', 'name', 'email', 'des']));
            if ($res) {
                return $this->returnJson(200, 'ok', $user->hidden(['password']));
            } else {
                return $this->returnJson(400, $res);
            }
        } catch (DataNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (ModelNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (DbException $e) {
            return $this->returnJson(500, $e->getMessage());
        }
    }

    /**
     * @param Request $request
     * @param Admin $model
     * @return \think\response\Json
     */
    public function indexRole(Request $request, Admin $model)
    {
        try {
            $res = $model->findOrFail(self::getUId())->roles()->with('perms')
                ->page($request::param('page', 1), $request::param('page', 10))
                ->paginate($request::param('page', 10));
            return $this->returnJson(200, 'ok', $res);
        } catch (DataNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (ModelNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (DbException $e) {
            return $this->returnJson(500, $e->getMessage());
        }
    }

    /**
     * @param Request $request
     * @param Admin $adminModel
     * @return \think\response\Json
     */
    public function hasPermission(\think\facade\Request $request, Admin $adminModel)
    {
        try {
            $res = $adminModel->findOrFail(self::getUId())->can($request::param('permission', ''));
            return $this->returnJson(200, 'ok', $res);
        } catch (DataNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (ModelNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (DbException $e) {
            return $this->returnJson(500, $e->getMessage());
        }
    }

    /**
     * @param \think\facade\Request $request
     * @param Admin $adminModel
     * @return \think\response\Json
     */
    public function changePwd(\think\facade\Request $request, Admin $adminModel)
    {
        $vil = Validate::make(['password' => 'require', 'new_password' => 'require']);
        $data = $request::only(['password', 'new_password']);
        if (!$vil->check($data)) {
            return $this->returnJson(422, $vil->getError());
        }
        try {
            $user = $adminModel->where(['password' => md5($data['password'])])->findOrFail(self::getUId());
            $user->password = md5($data['new_password']);
            if (!$user->save()) {
                return $this->returnJson(500, '失败');
            } else {
                return $this->returnJson(200, 'ok', $user->hidden(['password']));
            }
        } catch (DataNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (ModelNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (DbException $e) {
            return $this->returnJson(500, $e->getMessage());
        }
    }

    public function getUserInfo($id, Admin $adminModel)
    {
        return $this->returnJson(200, 'ok', $adminModel->findOrFail($id)->hidden(['password']));
    }
}
